<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('business.shipowner.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-row :gutter="20">
            <el-col :span="5">
              <el-form-item :label="i18n('business.shipowner.logo')" prop="logo">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.adornUrl('/file/picture')"
                  :show-file-list="false"
                  :headers="upload_headers"
                  :on-success="handleLogoSuccess"
                  :before-upload="beforePictureUpload">
                  <img v-if="dataForm.logo" :src="dataForm.logo" class="avatar-upload"/>
                  <el-icon v-else class="avatar-uploader-icon"><Plus/></el-icon>
                </el-upload>
              </el-form-item>

              <el-form-item :label="i18n('business.shipowner.share_url')" prop="share_url">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.adornUrl('/file/picture')"
                  :show-file-list="false"
                  :headers="upload_headers"
                  :on-success="handleShareUrlSuccess"
                  :before-upload="beforePictureUpload">
                  <img v-if="dataForm.share_url" :src="dataForm.share_url" class="avatar-upload"/>
                  <el-icon v-else class="avatar-uploader-icon"><Plus/></el-icon>
                </el-upload>
              </el-form-item>
            </el-col>
            <el-col :span="19">
              <el-form-item :label="i18n('business.shipowner.chinese_name')" prop="chinese_name">
                <el-input :placeholder="i18n('common.please_input') + i18n('business.shipowner.chinese_name')"
                  v-model="dataForm.chinese_name" show-word-limit maxlength="20"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.shipowner.english_name')" prop="english_name">
                <el-input :placeholder="i18n('common.please_input') + i18n('business.shipowner.english_name')" v-model="dataForm.english_name" show-word-limit maxlength="50"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.shipowner.scac')" prop="scac">
                <el-input :placeholder="i18n('common.please_input') + i18n('business.shipowner.scac')" v-model="dataForm.scac" show-word-limit maxlength="10"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.shipowner.chinese_short_name')" prop="chinese_short_name">
                <el-input :placeholder="i18n('common.please_input') + i18n('business.shipowner.chinese_short_name')" v-model="dataForm.chinese_short_name" show-word-limit maxlength="10"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.shipowner.english_short_name')" prop="english_short_name">
                <el-input :placeholder="i18n('common.please_input') + i18n('business.shipowner.english_short_name')" v-model="dataForm.english_short_name" show-word-limit maxlength="20"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.shipowner.address')" prop="address">
                <el-input type="textarea" :placeholder="i18n('common.please_input') + i18n('business.shipowner.address')" v-model="dataForm.address" show-word-limit maxlength="200"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.shipowner.mobile')" prop="mobile">
                <el-input :placeholder="i18n('common.please_input') + i18n('business.shipowner.mobile')" v-model="dataForm.mobile" show-word-limit maxlength="15"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.shipowner.email')" prop="email">
                <el-input :placeholder="i18n('common.please_input') + i18n('business.shipowner.email')" v-model="dataForm.email" show-word-limit maxlength="32"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.shipowner.detail.chinese_description')" prop="chinese_description">
                <el-input type="textarea" :placeholder="i18n('common.please_input') + i18n('business.shipowner.detail.chinese_description')" v-model="dataForm.chinese_description" show-word-limit maxlength="1000"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.shipowner.detail.english_description')" prop="english_description">
                <el-input type="textarea" :placeholder="i18n('common.please_input') + i18n('business.shipowner.detail.english_description')" v-model="dataForm.english_description" show-word-limit maxlength="1000"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('common.sort')" prop="sort">
                <el-input-number :placeholder="i18n('common.please_input') + i18n('common.sort')" v-model="dataForm.sort" :min="0"></el-input-number>
              </el-form-item>

              <el-form-item :label="i18n('business.shipowner.visit_status')" prop="visit_status">
                <el-switch
                  v-model="dataForm.visit_status"
                  active-value="1"
                  :active-text="i18n('common.yes')"
                  inactive-value="2"
                  :inactive-text="i18n('common.yes')">
                </el-switch>
              </el-form-item>

              <el-form-item :label="i18n('common.status')" prop="status">
                <el-switch
                  v-model="dataForm.status"
                  active-value="1"
                  :active-text="i18n('common.enable')"
                  inactive-value="2"
                  :inactive-text="i18n('common.disable')">
                </el-switch>
              </el-form-item>

              <el-form-item>
                <el-button v-if="isAuth('module:business:shipowner:handle')" type="primary" @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="business-shipowner-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useUpload } from "@/hooks/upload/useUpload"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/shipowner')

  const dataForm = reactive({
    id: 0,
    logo: '',
    scac: '',
    chinese_name: '',
    english_name: '',
    chinese_short_name: '',
    english_short_name: '',
    address: '',
    mobile: '',
    email: '',
    chinese_description: '',
    english_description: '',
    share_url: '',
    sort: 0,
    visit_status: '1',
    status: '1',
  })

  const dataRule = reactive({
    logo: [{
      required: true,
      message: i18n('business.shipowner.rules.logo.require'),
      trigger: 'blur',
    }],
    chinese_name: [{
      required: true,
      message: i18n('business.shipowner.rules.chinese_name.require'),
      trigger: 'blur',
    }],
    english_name: [{
      required: true,
      message: i18n('business.shipowner.rules.english_name.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const {
    upload_headers,
    handleLogoSuccess,
    beforePictureUpload,
  } = useUpload(dataForm, '')

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.logo = data.data.logo
            dataForm.scac = data.data.scac
            dataForm.chinese_name = data.data.chinese_name
            dataForm.english_name = data.data.english_name
            dataForm.chinese_short_name = data.data.chinese_short_name
            dataForm.english_short_name = data.data.english_short_name
            dataForm.address = data.data.address
            dataForm.mobile = data.data.mobile
            dataForm.email = data.data.email
            dataForm.share_url = data.data.share_url
            dataForm.sort = data.data.sort
            dataForm.visit_status = data.data.visit_status + ''
            dataForm.status = data.data.status.value + ''
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/detail/data`),
              method: 'get',
              params: $http.adornParams({
                'shipowner_id': dataForm.id
              }),
            }).then(({ data }) => {
              if (data.data && data.status === 200) {
                dataForm.chinese_description = data.data.chinese_description
                dataForm.english_description = data.data.english_description
              }
            })
          }
        })
      }
    })
  }

  const handleShareUrlSuccess = (res, file) => {
    dataForm.share_url = res.data
  }

  onMounted(() => {
    init(formRef)
  })
</script>
